//  https://ac.nowcoder.com/acm/problem/51024

#include<iostream>
#include<queue>
#include<cstring>
using namespace std;

const int N = 1010;
typedef pair<int, int> pii;
int n, m;
int a[N][N], dist[N][N];
queue<pii> q;
int dx[] = { 0,0,1,-1 };
int dy[] = { 1,-1,0,0 };

void bfs()
{

    while (q.size())
    {
        auto t = q.front();q.pop();
        int x = t.first, y = t.second;
        for (int k = 0;k < 4;k++)
        {
            int a = dx[k] + x, b = dy[k] + y;
            if (a >= 1 && a <= n && b >= 1 && b <= m && dist[a][b] == -1)
            {
                dist[a][b] = dist[x][y] + 1;
                q.push({ a,b });
            }
        }
    }
}

int main()
{
    memset(dist, -1, sizeof dist);
    cin >> n >> m;
    for (int i = 1;i <= n;i++)
    {
        for (int j = 1;j <= m;j++)
        {
            cin >> a[i][j];
            if (a[i][j] == 1)
            {
                q.push({ i,j });
                dist[i][j] = 0;
            }
        }
    }
    bfs();
    for (int i = 1;i <= n;i++)
    {
        for (int j = 1;j <= m;j++)
        {
            cout << dist[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}